﻿using Microsoft.EntityFrameworkCore;
using Polaris.Common;
using Polaris.Domain.Interfaces.Admin;
using Polaris.Domain.Model.DbEntity;
using Polaris.Domain.Model.Dto.Notes.Response;
using Polaris.Domain.Model.Dto.Said.Request;
using Polaris.Domain.Model.Entity.Base;
using Polaris.Domain.Repository.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace Polaris.Infrastructure.DomainService.Admin
{
    public class SaidService : ISaid
    {
        private readonly IBaseRepository<Said> _said;

        public SaidService(IBaseRepository<Said> said) { _said = said; }

        /// <summary>
        /// 创建说说
        /// </summary>
        /// <param name="create"></param>
        /// <param name="userId"></param>
        /// <exception cref="NotImplementedException"></exception>
        public async Task CreateSaid(CreateSaidRequest create, Guid userId)
        {
            var said = new Said()
            {
                Address = create.Address,
                Browse = 0,
                Content = create.Content,
                CreateBy = userId,
                CreatedTime = DateTime.Now,
                Id = Guid.NewGuid(),
                Images = create.Images,
                IsComments = create.IsComments,
                IsPrivacy = create.IsPrivacy,
                Like = 0
            };
            _said.AddEntity(said);
            await _said.CommitAsync();
        }

        /// <summary>
        /// 删除说说
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task DeleteSaid(string id, Guid userId)
        {
            var said = await _said.WhereLoadEntityAsync(d => d.Id == Guid.Parse(id));
            said.IsDelete = true;
            said.UpdatedBy = userId;
            said.UpdatedTime = DateTime.Now;
            _said.UpdateEntity(said);
            await _said.CommitAsync();
        }

        /// <summary>
        /// 根据id获取说说
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<SaidInfoResponse> GetSaidById(string id)
        {
            var said = await _said.QueryAll(d => d.Id == Guid.Parse(id))
                .Select(d => new SaidInfoResponse()
                {
                    Id = d.Id.ToString(),
                    Address = d.Address,
                    Browse = d.Browse,
                    Content = d.Content,
                    Images = d.Images,
                    IsComments = d.IsComments,
                    IsPrivacy = d.IsPrivacy,
                    Like = d.Like
                }).FirstOrDefaultAsync();

            return said;
        }

        /// <summary>
        /// 获取说说列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<PaginationInfo<SaidInfoResponse>> GetSaidList(SaidQueryRequest query)
        {
            var total = 0;
            var saidList = await _said.QueryAll(out total, query.PageIndex, query.PageSize, s => s.CreatedTime, false, s => !s.IsDelete)
                .Select(s => new SaidInfoResponse()
                {
                    Id = s.Id.ToString(),
                    Address = s.Address,
                    Browse = s.Browse,
                    Content = s.Content,
                    Images = s.Images,
                    IsComments = s.IsComments,
                    IsPrivacy = s.IsPrivacy,
                    Like = s.Like
                }).ToListAsync();

            var result = ApiResult
                   .CreatePaginationInfo<SaidInfoResponse>(
                                                               PageIndex: query.PageIndex,
                                                               PageSize: query.PageSize,
                                                               Total: total,
                                                               Result: saidList
                                                            );
            return result;
        }

        /// <summary>
        /// 修改说说
        /// </summary>
        /// <param name="update"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task UpdateSaid(SaidInfoResponse update, Guid userId)
        {
            var said = await _said.WhereLoadEntityAsync(d => d.Id == Guid.Parse(update.Id));
            said.IsPrivacy = update.IsPrivacy;
            said.UpdatedBy = userId;
            said.UpdatedTime = DateTime.Now;
            said.Address = update.Address;
            said.Content = update.Content;
            said.Images = update.Images;
            said.IsComments = update.IsComments;
            _said.UpdateEntity(said);
            await _said.CommitAsync();
        }
    }
}
